// import {listCustomer} from '../api/business/customer';
// import {listProduct} from "../api/business/product";

import {listCard} from '../api/business/card';
import {listSensor} from "../api/business/sensor";
import {listDevice} from "../api/business/device";
import {listChannel} from "../api/business/channel";
import {listVideo_channels} from "../api/business/video_channels";


const OPTIONS_DATA_MAP = {
  // customer: listCustomer,
  // product: listProduct,
  "SENSOR": listSensor,
  "ACQUISITION_CARD": listCard,
  "DEVICE": listDevice,
  "CHANNEL": listChannel,
  "VIDEO_CHANNEL": listVideo_channels
}

// 全局是否可缓存，不传默认开启缓存
let GLOBAL_CACHEABLE = true;

export default {

  // 根据key获取options data
  getOptions: async function (key, param, cacheable) {
    // 逻辑验证
    if (cacheable === undefined) {
      cacheable = GLOBAL_CACHEABLE;
    }
    if (cacheable) {
      let tempKey = key;
      if (param) {
        tempKey = key + JSON.stringify(param);
      }
      let optionsDataString = sessionStorage.getItem(tempKey);
      if (optionsDataString) {
        let data = JSON.parse(optionsDataString);
        console.log("使用缓存", JSON.parse(optionsDataString));
        if (data instanceof Array) {
          if (data.length !== 0) {
            return JSON.parse(optionsDataString);
          }
        } else {
          return JSON.parse(optionsDataString);
        }
      }
    }
    console.log("业务下拉数据" + key + "尚未被缓存或禁用缓存，获取远端数据中...");
    console.log(OPTIONS_DATA_MAP[key])
    let { rows } = await OPTIONS_DATA_MAP[key](param || {});
    sessionStorage.setItem(param ? key + JSON.stringify(param) : key, JSON.stringify(rows));
    return rows;
  },
  getLabel: function (val, dataList, valKey, labelKey) {
    let filter = dataList.filter(item => item[valKey] === val);
    return filter[0][labelKey];
  },
  getItem: function (val, dataList, valKey) {
    let filter = dataList.filter(item => item[valKey] === val);
    return filter[0];
  }


}
